package com.zhiyun.yycall.version;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.zhiyun.yycall.R;


public class VersionDialogActivity extends Activity implements OnClickListener {

	private static final String TAG_POSITIVE = "positive";
	private static final String TAG_NEGATIVE = "negative";
	public static final String INT_KEY_TITILE = "dlg_title";
	public static final String INT_KEY_CONTENT = "dlg_content";
	public static final String INT_KEY_BTN_POSITIVE = "btn_positive";
	public static final String INT_KEY_BTN_NEGATIVE = "btn_negative";
	public static final String INT_KEY_TASK = "dlg_task";
	public static final int TASK_CHECK_VERSION = 0;
	public static final int TASK_DOWNLOAD_ERR = 1;
	public static final int TASK_DOWNLOAD_ASK = 2;

	private TextView dlgTitle;
	private TextView dlgContent;
	private Button positiveBtn;
	private Button negativeBtn;

	private int taskType = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.version_dialog_activity);

		initComponents();
		setData();
	}

	/** 取消下载通知 **/
	private void cancelNoti(int notificationId) {
		NotificationManager manager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
		manager.cancel(notificationId);
	}

	/**
	 * 初始化化组件
	 */
	private void initComponents() {
		dlgTitle = (TextView) findViewById(R.id.title);
		dlgContent = (TextView) findViewById(R.id.content);
		positiveBtn = (Button) findViewById(R.id.positiveBtn);
		positiveBtn.setOnClickListener(this);
		positiveBtn.setTag(TAG_POSITIVE);
		negativeBtn = (Button) findViewById(R.id.negativeBtn);
		negativeBtn.setOnClickListener(this);
		negativeBtn.setTag(TAG_NEGATIVE);
	}

	/**
	 * 把对话框的头信息、文本信息设置进去
	 */
	private void setData() {

		// 如果有传进标题信息
		String title = getIntent().getStringExtra(INT_KEY_TITILE);
		if (title != null) {
			setDlgTitle(title);
		}
		// 如果有传进提示内容信息
		String content = getIntent().getStringExtra(INT_KEY_CONTENT);
		if (content != null) {
			setDlgContent(content);
		}
		// 如果有传进确认按钮名称
		String posBtnText = getIntent().getStringExtra(INT_KEY_BTN_POSITIVE);
		if (posBtnText != null) {
			positiveBtn.setText(posBtnText);
		}
		// 如果有传进取消按钮名称
		String negBtnText = getIntent().getStringExtra(INT_KEY_BTN_NEGATIVE);
		if (negBtnText != null) {
			negativeBtn.setText(negBtnText);
		}
		taskType = getIntent().getIntExtra(INT_KEY_TASK, -1);
	}

	public void setDlgTitle(String title) {
		if (title != null) {
			dlgTitle.setText(title);
		}
	}

	public void setDlgContent(String content) {
		if (content != null) {
			dlgContent.setText(content);
		}
	}

	@Override
	public void onClick(View v) {
		if (TAG_POSITIVE.equals(v.getTag())) {
			if (taskType == TASK_CHECK_VERSION) {
				VersionManager.getInstance().startDownload();
				cancelNoti(VersionService.FOUND_NEW_VERSION_NOTI_ID);
			} else if (taskType == TASK_DOWNLOAD_ERR) {
				VersionManager.getInstance().startDownload();
				cancelNoti(VersionService.DOWNLOAD_NOTI_ID);
			} else if (taskType == TASK_DOWNLOAD_ASK) {
				VersionManager.getInstance().interruptTask();
				cancelNoti(VersionService.DOWNLOAD_NOTI_ID);
			}
		} else if (TAG_NEGATIVE.equals(v.getTag())) {
			if (taskType == TASK_CHECK_VERSION) {
				cancelNoti(VersionService.FOUND_NEW_VERSION_NOTI_ID);
			} else if (taskType == TASK_DOWNLOAD_ERR) {
				cancelNoti(VersionService.DOWNLOAD_NOTI_ID);
			} else if (taskType == TASK_DOWNLOAD_ASK) {
				cancelNoti(VersionService.DOWNLOAD_NOTI_ID);
			}
		} else {
			Log.i("DialogChoiceActivity", "未知的事件响应...");
		}
		finish();
	}

	/**
	 * 屏蔽返回按钮
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
